﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Imports AugmentLib.WebComponent
Imports AugmentLib.InputOutput.Encryption

Partial Class MobileBuyRent
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 25
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
        LoadUpload()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
        LoadUpload()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
        LoadUpload()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
        LoadUpload()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
        LoadUpload()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
        LoadUpload()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
        LoadUpload()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
            LoadUpload()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
        LoadUpload()
    End Sub

#End Region

    Protected Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        Session("anchor1") = ""
        LoadUpload()
    End Sub

    Protected Sub btnResetSearchFilter_Click(sender As Object, e As EventArgs) Handles btnResetSearchFilter.Click
        CurrentPage = 0
        txtLocationKeyword.Text = String.Empty
        txtName.Text = String.Empty
        txtPhone.Text = String.Empty
        txtEmail.Text = String.Empty
        txtCompany.Text = String.Empty
        txtCreatedDateMin.Text = String.Empty
        txtCreatedDateMax.Text = String.Empty
        ddlStatusDelete.SelectedIndex = 0
        ddlSubmissionType.SelectedIndex = 0
        ddlUseInvest.SelectedIndex = 0
        ddlPropertyType.SelectedIndex = 0
    End Sub

    Private Sub LoadUpload()
        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable
        divError.Visible = False
        divSuccessful.Visible = False

        Try
            objDB.OpenConnection()

            Dim LocationKeyword, Name, Phone, Email, SubmissionType, PropertyType, UseInvest, Company,
                CreatedDateMin, CreatedDateMax As String

            If txtLocationKeyword.Text <> String.Empty Then
                LocationKeyword = " AND LocationKeyword LIKE @LocationKeyword"
                objDB.AddParameter("LocationKeyword", Data.SqlDbType.NVarChar, "%" & txtLocationKeyword.Text.Trim & "%")
            Else
                LocationKeyword = String.Empty
            End If

            If txtName.Text <> String.Empty Then
                Name = " AND Name LIKE @Name"
                '" AND LocationKeyword LIKE '%" + txtLocationKeyword.Text.Replace(",", "%' AND [Title] LIKE '%") + "%'"
                objDB.AddParameter("Name", Data.SqlDbType.NVarChar, "%" & txtName.Text.Trim & "%")
            Else
                Name = String.Empty
            End If

            If txtPhone.Text.Trim <> String.Empty Then
                Phone = " AND Phone LIKE @Phone"
                objDB.AddParameter("Phone", Data.SqlDbType.NVarChar, "%" & txtPhone.Text.Trim & "%")
            Else
                Phone = String.Empty
            End If

            If txtEmail.Text <> String.Empty Then
                Email = " AND Email LIKE @Email"
                objDB.AddParameter("Email", Data.SqlDbType.NChar, "%" & txtEmail.Text.Trim & "%")
            Else
                Email = String.Empty
            End If

            If txtCompany.Text <> String.Empty Then
                Company = " AND Company LIKE @Company"
                objDB.AddParameter("Company", Data.SqlDbType.NVarChar, "%" & txtCompany.Text.Trim & "%")
            Else
                Company = String.Empty
            End If

            If ddlSubmissionType.SelectedValue <> String.Empty Then
                SubmissionType = " AND SubmissionType LIKE  @SubmissionType"
                objDB.AddParameter("SubmissionType", Data.SqlDbType.NVarChar, "%" & ddlSubmissionType.SelectedItem.Text & "%")
            Else
                SubmissionType = String.Empty
            End If
            If ddlPropertyType.SelectedValue <> String.Empty Then
                PropertyType = " AND PropertyType LIKE  @PropertyType"
                objDB.AddParameter("PropertyType", Data.SqlDbType.NVarChar, "%" & ddlPropertyType.SelectedItem.Text & "%")
            Else
                PropertyType = String.Empty
            End If
            If ddlUseInvest.SelectedValue <> String.Empty Then
                UseInvest = " AND UseInvest LIKE  @UseInvest"
                objDB.AddParameter("UseInvest", Data.SqlDbType.NVarChar, "%" & ddlUseInvest.SelectedItem.Text & "%")
            Else
                UseInvest = String.Empty
            End If
            If txtCreatedDateMin.Text.Trim <> String.Empty Then
                CreatedDateMin = " AND CreatedDate >= @CreatedDateMin"
                objDB.AddParameter("CreatedDateMin", Data.SqlDbType.Date, txtCreatedDateMin.Text.Trim)
            Else
                CreatedDateMin = String.Empty
            End If

            If txtCreatedDateMax.Text.Trim <> String.Empty Then
                CreatedDateMax = " AND CreatedDate <= @CreatedDateMax"
                objDB.AddParameter("CreatedDateMax", Data.SqlDbType.Date, txtCreatedDateMax.Text.Trim)
            Else
                CreatedDateMax = String.Empty
            End If

            objDB.AddParameter("StatusDelete", Data.SqlDbType.TinyInt, ddlStatusDelete.SelectedValue)

            objDB.dbVariable.SQLString = "SELECT ID,Name,Phone,Email,Company,LocationKeyword, PropertyType,UseInvest" & _
                                        ",Price,CreatedDate,SubmissionType,Remark from Mobile_CustomerWantBuyRent " & _
                                        "WHERE isDelete = @StatusDelete " & _
                                        LocationKeyword & Name & Phone & Email & _
                                         SubmissionType & PropertyType & UseInvest & Company & _
                                        CreatedDateMin & CreatedDateMax & _
                                        lblhidSortType.Text

            objDB.FillData(TempDT, "Mobile_CustomerWantBuyRent", Database.SQLCommandType.NormalString)

            objDB.CloseConnection()

            'Creating a Paged Datasouce
            Dim objPaged As New PagedDataSource
            objPaged.DataSource = TempDT.DefaultView
            objPaged.AllowPaging = True
            objPaged.PageSize = PageSize
            objPaged.CurrentPageIndex = CurrentPage

            lblHeadNoOfItem.Text = TempDT.Rows.Count
            lblFootNoOfItem.Text = TempDT.Rows.Count

            repUpload.DataSource = objPaged
            repUpload.DataBind()

            lblMaxPageCount.Text = objPaged.PageCount - 1
            lblTotalPage.Text = " of " & objPaged.PageCount

        Catch ex As Exception
            divError.Visible = True
            lblMsgError.Text = ex.Message.ToString
        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
    End Sub

    Private Function SortType(ByVal strSortType As String) As String
        Dim strSort As String
        If Right(strSortType, 1) = "▼" Then
            strSort = "ASC"
        Else
            strSort = "DESC"
        End If
        Return strSort
    End Function

    Protected Sub lnkLocationKeyword_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLocationKeyword.Click
        If lnkLocationKeyword.Text = "LocationKeyword▼" Then
            lnkLocationKeyword.Text = "LocationKeyword▲"
        Else
            lnkLocationKeyword.Text = "LocationKeyword▼"
        End If

        lblhidSortType.Text = " ORDER BY LocationKeyword " & SortType(lnkLocationKeyword.Text) & _
                            ", CreatedDate " & SortType(lnkCreatedDate.Text) & _
                            ", Name " & SortType(lnkName.Text) & _
                            ", Phone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkName_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkName.Click
        If lnkName.Text = "Name▼" Then
            lnkName.Text = "Name▲"
        Else
            lnkName.Text = "Name▼"
        End If

        lblhidSortType.Text = " ORDER BY Name " & SortType(lnkName.Text) & _
                            ", CreatedDate " & SortType(lnkCreatedDate.Text) & _
                            ", LocationKeyword " & SortType(lnkLocationKeyword.Text) & _
                            ", Phone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkPhone_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPhone.Click
        If lnkPhone.Text = "Phone▼" Then
            lnkPhone.Text = "Phone▲"
        Else
            lnkPhone.Text = "Phone▼"
        End If

        lblhidSortType.Text = " ORDER BY Phone " & SortType(lnkPhone.Text) & _
                            ", CreatedDate " & SortType(lnkCreatedDate.Text) & _
                            ", LocationKeyword " & SortType(lnkLocationKeyword.Text) & _
                            ", Email " & SortType(lnkEmail.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkEmail.Click
        If lnkEmail.Text = "Email▼" Then
            lnkEmail.Text = "Email▲"
        Else
            lnkEmail.Text = "Email▼"
        End If

        lblhidSortType.Text = " ORDER BY Email " & SortType(lnkEmail.Text) & _
                            ", CreatedDate " & SortType(lnkCreatedDate.Text) & _
                            ", LocationKeyword " & SortType(lnkLocationKeyword.Text) & _
                            ", Phone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkSubmissionType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkSubmissionType.Click
        If lnkSubmissionType.Text = "Submission Type▼" Then
            lnkSubmissionType.Text = "Submission Type▲"
        Else
            lnkSubmissionType.Text = "Submission Type▼"
        End If

        lblhidSortType.Text = " ORDER BY SubmissionType " & SortType(lnkSubmissionType.Text) & _
                            ", CreatedDate " & SortType(lnkCreatedDate.Text) & _
                            ", LocationKeyword " & SortType(lnkLocationKeyword.Text) & _
                            ", Phone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub
    Protected Sub lnkPropertyType_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPropertyType.Click
        If lnkPropertyType.Text = "Property Type▼" Then
            lnkPropertyType.Text = "Property Type▲"
        Else
            lnkPropertyType.Text = "Property Type▼"
        End If

        lblhidSortType.Text = " ORDER BY PropertyType " & SortType(lnkPropertyType.Text) & _
                            ", CreatedDate " & SortType(lnkCreatedDate.Text) & _
                            ", LocationKeyword " & SortType(lnkLocationKeyword.Text) & _
                            ", Phone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkUseInvest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkUseInvest.Click
        If lnkPropertyType.Text = "For Invest▼" Then
            lnkPropertyType.Text = "For Invest▲"
        Else
            lnkPropertyType.Text = "For Invest▼"
        End If

        lblhidSortType.Text = " ORDER BY UseInvest " & SortType(lnkPropertyType.Text) & _
                            ", CreatedDate " & SortType(lnkCreatedDate.Text) & _
                            ", LocationKeyword " & SortType(lnkLocationKeyword.Text) & _
                            ", Phone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkCompany_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCompany.Click
        If lnkCompany.Text = "Company▼" Then
            lnkCompany.Text = "Company▲"
        Else
            lnkCompany.Text = "Company▼"
        End If

        lblhidSortType.Text = " ORDER BY Company " & SortType(lnkCompany.Text) & _
                            ", CreatedDate " & SortType(lnkCreatedDate.Text) & _
                            ", LocationKeyword " & SortType(lnkLocationKeyword.Text) & _
                            ", Phone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkCreatedDate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCreatedDate.Click
        If lnkCreatedDate.Text = "Submission Date▼" Then
            lnkCreatedDate.Text = "Submission Date▲"
        Else
            lnkCreatedDate.Text = "Submission Date▼"
        End If

        lblhidSortType.Text = " ORDER BY CreatedDate " & SortType(lnkCreatedDate.Text) & _
                            ", LocationKeyword " & SortType(lnkLocationKeyword.Text) & _
                            ", Phone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub

    Protected Sub lnkPrice_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkPrice.Click
        If lnkPrice.Text = "Price▼" Then
            lnkPrice.Text = "Price▲"
        Else
            lnkPrice.Text = "Price▼"
        End If

        lblhidSortType.Text = " ORDER BY Price " & SortType(lnkPrice.Text) & _
                            ", CreatedDate " & SortType(lnkCreatedDate.Text) & _
                            ", LocationKeyword " & SortType(lnkLocationKeyword.Text) & _
                            ", Phone " & SortType(lnkPhone.Text)

        btnSearch_Click(sender, e)
    End Sub


    Protected Sub repUpload_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repUpload.ItemCommand
        If e.CommandName = "ViewList" Then
            'Edited by minginn - To allow user to go back to the search page with same search selection
            '1-Back Page
            '2-Page Size
            '3-Current Page

            Dim strBack As String
            strBack = "MobileSellLet.aspx" & "ڡ" & PageSize & "ڡ" & CurrentPage & "ڡ" & txtLocationKeyword.Text.Trim & "ڡ" & _
                    txtName.Text.Trim & "ڡ" & txtPhone.Text.Trim & "ڡ" & txtEmail.Text.Trim & "ڡ" & txtCompany.Text.Trim & "ڡ" & _
                    ddlSubmissionType.SelectedValue.Trim & "ڡ" & ddlPropertyType.SelectedValue.Trim & ddlUseInvest.SelectedValue.Trim & "ڡ" & "ڡ" & txtCreatedDateMin.Text.Trim & "ڡ" & txtCreatedDateMax.Text.Trim & "ڡ" & ddlStatusDelete.SelectedValue.Trim

            Session.Add("Back", strBack)
            'Response.Redirect("AuctionEntry.aspx?id=" & SymmetricEncryptToHex(e.CommandArgument, "augment"))
            'Response.Redirect("RecentTransactionEntry.aspx?id=" & e.CommandArgument)
        End If
        If e.CommandName.ToUpper = "DELETEITEM" Then

            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "UPDATE Mobile_CustomerWantBuyRent SET isDelete=1 WHERE Id=@Id"
                objDB.AddParameter("Id", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repUpload.Items(e.CommandArgument).FindControl("lblRepId"), Label).Text))
                objDB.ExecuteQuery()
                objDB.CloseConnection()
                lblMsgSuccessful.Text = "Entry Deleted"
                lblMsgSuccessful.Visible = True
                divSuccessful.Visible = True
                LoadUpload()
            Catch ex As Exception
                lblMsgError.Text = "Entry Not Deleted. Error : " + ex.Message
                divError.Visible = True
            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        End If
    End Sub

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Session.Item("StaffId") Is Nothing Then
            Response.Redirect("default.aspx")
        End If

        'Edited by minginn @ 27/6/2011 - to load the same search when user back from other page
        If Not Session("Back") Is Nothing Then
            Dim strBack As String = Session("Back").ToString
            Dim arr() As String = strBack.Split("ڡ")

            If arr(0).ToString.Contains("MobileSellLet.aspx") Then
                PageSize = CInt(arr(1).ToString)
                CurrentPage = CInt(arr(2).ToString)
                txtLocationKeyword.Text = arr(3).ToString
                txtName.Text = arr(4).ToString
                txtPhone.Text = arr(5).ToString
                txtEmail.Text = arr(6).ToString
                txtCompany.Text = arr(7).ToString
                ddlSubmissionType.SelectedValue = arr(8).ToString
                ddlPropertyType.SelectedValue = arr(9).ToString
                ddlUseInvest.SelectedValue = arr(10).ToString
                txtCreatedDateMin.Text = arr(11).ToString
                txtCreatedDateMax.Text = arr(12).ToString
                ddlStatusDelete.SelectedValue = arr(13).ToString
                Session.Remove("Back")
                LoadUpload()

            Else
                Session.Remove("Back")
            End If
        Else
            If Not Page.IsPostBack Then

                lblStaffUID.Text = Session.Item("StaffId").ToString
                lblStaffType.Text = Session.Item("StaffType").ToString
                lnkCreatedDate_Click(sender, e)

            End If
        End If

        If Not Page.IsPostBack Then

            lblStaffUID.Text = Session.Item("StaffId").ToString
            lblStaffType.Text = Session.Item("StaffType").ToString

        End If
    End Sub


End Class
